package jomm.utils.db.mysql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * User: Jorge Machado
 * Date: 17/Fev/2010
 * Time: 10:29:13
 */
public class ConnectionManager {
    private String URL;
    private String DRIVER;
    private String UserName;
    private String Pass;

    private Connection conn;
    static Map<String, Connection> connections = new HashMap<String, Connection>();

    public ConnectionManager(String catalog, String username, String password) {
        URL = "jdbc:mysql://localhost/" + catalog + "?autoReconnect=true";
        DRIVER = "com.mysql.jdbc.Driver";
        UserName = username;
        Pass = password;
    }

    public ConnectionManager(String host, String catalog, String username, String password) {
        URL = "jdbc:mysql://" + host + "/" + catalog + "?autoReconnect=true";
        DRIVER = "com.mysql.jdbc.Driver";
        UserName = username;
        Pass = password;
    }
    public ConnectionManager(String host, String catalog, String username, String password, String rest) {
        URL = "jdbc:mysql://" + host + "/" + catalog + rest;
        DRIVER = "com.mysql.jdbc.Driver";
        UserName = username;
        Pass = password;
    }


    public synchronized Connection getConnection(boolean autocommit) throws SQLException {
        if (conn != null) {
            if (!conn.isClosed() && conn.isValid(100000))
                return conn;
            if (!conn.isClosed())
                conn.close();
            conn = null;
        }

        try {
//            System.out.println("CONNNNNTTTTCONS+++++" + (++COUNT2));
            Class.forName(DRIVER);
            conn = DriverManager.getConnection(URL, UserName, Pass);
            conn.setAutoCommit(autocommit);
            return conn;
        }
        catch (ClassNotFoundException e) {
            throw new SQLException(e.getMessage());
        }
    }
//    static int COUNT = 0;
//    static int COUNT2 = 0;


    public synchronized Connection newConnection(boolean autocommit) throws SQLException
    {
    	try {
	    	 Class.forName(DRIVER);
	         conn = DriverManager.getConnection(URL, UserName, Pass);
	         conn.setAutoCommit(autocommit);
	         return conn;
	    }
	    catch (ClassNotFoundException e) {
	        throw new SQLException(e.getMessage());
	    }
    }
    
    public synchronized Connection getConnection(boolean autocommit, String key) throws SQLException {
        Connection conn = connections.get(key);
        if (conn != null) {
            if (!conn.isClosed() && conn.isValid(5000))
                return conn;
            if (!conn.isClosed())
                conn.close();
            conn = null;
            connections.put(key, conn);
        }

        try {
//            System.out.println("CONNNNNTTTTCONS+++++" + (++COUNT2));
            Class.forName(DRIVER);
            conn = DriverManager.getConnection(URL, UserName, Pass);
            conn.setAutoCommit(autocommit);
            connections.put(key, conn);
            return conn;
        }
        catch (ClassNotFoundException e) {
            throw new SQLException(e.getMessage());
        }
    }

    public synchronized Connection getConnection(String key) throws SQLException {
        return getConnection(true, key);
    }

    public synchronized Connection getConnection() throws SQLException {
        return getConnection(true);
    }
}
